/*
 * Copyright 2006 Abdulla G. Abdurakhmanov (abdulla.abdurakhmanov@gmail.com).
 * 
 * Licensed under the GPL, Version 2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.gnu.org/copyleft/gpl.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * With any your questions welcome to my e-mail 
 * or blog at http://abdulla-a.blogspot.com.
 */

package org.ofsm.creator;

import javax.swing.table.AbstractTableModel;

import org.ofsm.impl.FileFiniteStateMachineBaseModel;
import org.ofsm.impl.FileFiniteStateMachineModel;
import org.ofsm.impl.StateTransition;

public class StatesTableModel extends AbstractTableModel{
    private FileFiniteStateMachineModel model;
    public StatesTableModel(FileFiniteStateMachineModel model) {
        this.model = model;
    }
    
    public int getColumnCount() {
        return model.getEventInfos().size() + 1; 
    } 
    
    public int getRowCount() { 
        return model.getStateInfos().size();
    } 
    
    private FileFiniteStateMachineModel.StateInfo getStateInfoByRow(int row) {
        return (FileFiniteStateMachineModel.StateInfo)model.getStateInfos().toArray()[row];
    }

    private FileFiniteStateMachineModel.EventInfo getEventInfoByCol(int col) {
        int realCol = col -1;
        return (FileFiniteStateMachineModel.EventInfo)model.getEventInfos().toArray()[realCol];
    }
    
    public Object getValueAt(int row, int col) {
        if(col == 0)
            return model.getStateInfos().toArray()[row];
        else {
            StateTransition trans = 
                model.getTransitionInfo(getStateInfoByRow(row).getState(),getEventInfoByCol(col).getEvent());
            if(trans!=null) {
                FileFiniteStateMachineModel.StateInfo result = model.getStateInfo(trans.getToState().getName());
                return result;
            }
            else
                return "...";
        }
    } 
    public String getColumnName(int column) {
        if(column == 0)
            return "...";
        else
            return model.getEventInfos().toArray()[column-1].toString();
    } 
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();                
    } 
    public boolean isCellEditable(int row, int col) { 
        if(col == 0)
            return false;
        else
            return true;
    } 
    public void setValueAt(Object aValue, int row, int col) { 
        StateTransition trans = 
            model.getTransitionInfo(getStateInfoByRow(row).getState(),getEventInfoByCol(col).getEvent());
        if(trans!=null) {
            if(aValue instanceof FileFiniteStateMachineModel.StateInfo) {
                trans.setToState(((FileFiniteStateMachineModel.StateInfo)aValue).getState());
            }
            else {
                model.deleteTransitionInfo(trans.getFromState(),trans.getEvent());
            }
        }
        else {
            if(aValue instanceof FileFiniteStateMachineModel.StateInfo) {
                trans = new StateTransition();
                trans.setFromState(getStateInfoByRow(row).getState());
                trans.setToState(((FileFiniteStateMachineModel.StateInfo)aValue).getState());
                trans.setEvent(getEventInfoByCol(col).getEvent());
                model.addTransitionInfo(trans);        
            }
        }
    }  
}
